package com.entfrm.wxmp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;
import com.entfrm.base.constant.AppConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpFreePublishService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信发布
 *
 * @author www.joolun.com
 * @date 2022-03-10 21:26:35
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(AppConstants.APP_WXMP + "/wxPublish")
@Api("微信发布")
public class WxPublishController {
	private final WxMpService wxService;

	/**
	 * 获取成功发布列表
	 *
	 * @param page 获取成功发布列表
	 * @param
	 * @return
	 */
	@ApiOperation("获取成功发布列表")
	@GetMapping("/list")
	@SaCheckPermission("wxPublish_view')")
	public R list(Page page) throws Exception {
		WxMpFreePublishService wxMpFreePublishService = wxService.getFreePublishService();
		int count = (int) page.getSize();
		int offset = (int) page.getCurrent() * count - count;
		return R.ok(wxMpFreePublishService.getPublicationRecords(offset, count));
	}

	/**
	 * 删除发布
	 *
	 * @param
	 * @return R
	 */
	@ApiOperation("删除发布")
	@DeleteMapping
	@SaCheckPermission("wxPublish_del')")
	public R del(String id) throws Exception {
		WxMpFreePublishService wxMpFreePublishService = wxService.getFreePublishService();
		return R.ok(wxMpFreePublishService.deletePushAllArticle(id));
	}

}
